import 'package:flutter/material.dart';

enum SortOption {
  title, // 标题
  deadline, // 截止日期
  priority, // 优先级
  updateTime, // 更新时间
}

class SortDropdown extends StatefulWidget {
  final Function(SortOption)? onSortChanged;
  final SortOption? initialSort;

  const SortDropdown({Key? key, this.onSortChanged, this.initialSort})
    : super(key: key);

  @override
  _SortDropdownState createState() => _SortDropdownState();
}

class _SortDropdownState extends State<SortDropdown> {
  late SortOption _selectedSort;

  @override
  void initState() {
    super.initState();
    _selectedSort = widget.initialSort ?? SortOption.updateTime;
  }

  // 获取排序选项的显示文本
  String _getSortText(SortOption option) {
    switch (option) {
      case SortOption.deadline:
        return '截止日期';
      case SortOption.priority:
        return '优先级';
      case SortOption.updateTime:
        return '修改时间';
      case SortOption.title:
        return '标题';
    }
  }

  // 获取排序选项的图标
  IconData _getSortIcon(SortOption option) {
    switch (option) {
      case SortOption.deadline:
        return Icons.date_range;
      case SortOption.priority:
        return Icons.flag;
      case SortOption.title:
        return Icons.title;
      case SortOption.updateTime:
        return Icons.access_time;
    }
  }

  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;
    return Container(
      height: 30,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8.0),
        // ignore: deprecated_member_use
        border: Border.all(color: Colors.grey.withOpacity(0.3)),
        color: colorScheme.surface,
      ),
      child: Theme(
        data: Theme.of(context).copyWith(
          canvasColor: colorScheme.onSecondary, // 设置下拉菜单背景色
        ),
        child: DropdownButtonHideUnderline(
          child: ButtonTheme(
            alignedDropdown: true,
            child: DropdownButton<SortOption>(
              value: _selectedSort,
              icon: Icon(Icons.arrow_drop_down, color: colorScheme.secondary),
              iconSize: 10,
              elevation: 16,
              style: TextStyle(color: colorScheme.secondary, fontSize: 15),
              onChanged: (SortOption? newValue) {
                if (newValue != null) {
                  setState(() {
                    _selectedSort = newValue;
                  });
                  widget.onSortChanged?.call(newValue);
                }
              },
              items:
                  SortOption.values.map<DropdownMenuItem<SortOption>>((
                    SortOption value,
                  ) {
                    return DropdownMenuItem<SortOption>(
                      value: value,
                      child: Row(
                        children: [
                          Icon(
                            _getSortIcon(value),
                            color: colorScheme.onSurface,
                            size: 20,
                          ),
                          const SizedBox(width: 12),
                          Text(
                            _getSortText(value),
                            // style: TextStyle(fontWeight: FontWeight.w500),
                          ),
                        ],
                      ),
                    );
                  }).toList(),
              // 自定义下拉按钮样式
              selectedItemBuilder: (BuildContext context) {
                return SortOption.values.map((SortOption value) {
                  return Container(
                    padding: const EdgeInsets.all(2),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Icon(_getSortIcon(value), color: colorScheme.onSurface),
                        SizedBox(width: 10),
                        Text(
                          _getSortText(value),
                          // style: const TextStyle(fontWeight: FontWeight.w500),
                        ),
                      ],
                    ),
                  );
                }).toList();
              },
            ),
          ),
        ),
      ),
    );
  }
}
